#include "UI.h"
#include "../ACUAS/projectdefinitions.h"

#include <stdio.h>

void
ui_initialize()
{
  lcd_init(LCD_DISP_ON);

  // Define special character "Heart"
  LCDcharDef(1, 0x00, 0x1B, 0x1F, 0x1F, 0x0E, 0x04, 0x00, 0x00);
  // Define special character "Diamond"
  LCDcharDef(2, 0x04, 0x0E, 0x1F, 0x1F, 0x1F, 0x0E, 0x04, 0x00);
  // Define special character "Club"
  LCDcharDef(3, 0x0E, 0x0E, 0x04, 0x1F, 0x1F, 0x04, 0x04, 0x0E);
  // Define special character "Spade"
  LCDcharDef(4, 0x00, 0x04, 0x0E, 0x1F, 0x1F, 0x1F, 0x04, 0x0E);
  // Define special character "Full_Case"
  LCDcharDef(5, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F);
  // Define special character "LINE_LEFT"
  LCDcharDef(6, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F);
  // Define special character "LINE_RIGHT"
  LCDcharDef(7, 0x1F, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1F);
  // Define special character "LINE_UP_DOWN"
  LCDcharDef(8, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F);
}

static void
ui_draw_card(Card card)
{
  static const unsigned char suites[] = { 0, 0, 1, 2, 3 };
  static const unsigned char values[] =
    { 0, 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };

  // Get card suite and value
  unsigned char suite = card_get_suite(card);
  unsigned char value = card_get_value(card);

  // Check if the card is valid
  if (suite == 0 || value == 0)
    return;

  lcd_putc(values[value]);
  lcd_putc(suites[suite]);
}

void
ui_chips(unsigned int chips)
{
  char buffer[20];
  lcd_gotoxy(0, 0);
  lcd_puts("Chips: ");
  if (chips) {
    sprintf(buffer, "%u", chips);
    lcd_puts(buffer);
  } else {
    lcd_puts("???");
  }
  lcd_puts("   ");
}

void
ui_count(int count)
{
  char buffer[20];
  lcd_gotoxy(0, 1);
  sprintf(buffer, "%+d", count);
  lcd_puts("Count: ");
  lcd_puts(buffer);
  lcd_puts("   ");
}

void
ui_card(Card card)
{
  static unsigned char pos = 1;
  lcd_gotoxy(pos, 3);
  ui_draw_card(card);

  pos += 3;
  if (pos > 14)
    pos = 1;
}
